package com.youdo.careyes.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.youdo.careyes.R;
import com.youdo.careyes.databinding.FragmentToolsBinding;
import com.youdo.careyes.model.CameraChangeEvent;
import com.youdo.careyes.utils.ToastUtil;
import com.youdo.helper.UVCCameraHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 工具条
 *
 * @author ljb
 * @date 2025.07.30
 * @email 568838953@qq.com
 */
public class ToolsFragment extends Fragment {
    private final UVCCameraHelper mCameraHelper = UVCCameraHelper.getInstance();
    private FragmentToolsBinding binding;
    private boolean isRecord;
    private boolean isOpenSound;
    private boolean isInitView;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentToolsBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.floatingActionSnap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraHelper.takePicture();
                ToastUtil.showLong(R.string.app_public_take_picture_success);
            }
        });
        binding.floatingActionRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraHelper.recordVideo(!mCameraHelper.isRecording());
            }
        });
        binding.floatingActionSound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOpenSound = !isOpenSound;
                if (isOpenSound) {
                    binding.floatingActionSound.setImageResource(R.drawable.floating_action_btn_on_sound);
                } else {
                    binding.floatingActionSound.setImageResource(R.drawable.floating_action_btn_off_sound);
                }
            }
        });
        binding.floatingActionSet.setOnClickListener(v -> NavHostFragment.findNavController(ToolsFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment));
        binding.floatingActionFiles.setOnClickListener(v -> {
            NavHostFragment.findNavController(ToolsFragment.this).navigate(R.id.action_FirstFragment_to_ThirdFragment);
        });
        if (!isInitView) {
            isInitView = true;
            EventBus.getDefault().register(this);
        }
        binding.floatingActionSnap.setNextFocusDownId(binding.floatingActionRecord.getId());
        binding.floatingActionRecord.setNextFocusDownId(binding.floatingActionSet.getId());
        binding.floatingActionRecord.setNextFocusUpId(binding.floatingActionSnap.getId());
        binding.floatingActionSet.setNextFocusUpId(binding.floatingActionRecord.getId());
        binding.floatingActionSet.setNextFocusDownId(binding.floatingActionFiles.getId());
        binding.floatingActionFiles.setNextFocusUpId(binding.floatingActionSet.getId());
    }


    @Override
    public void onResume() {
        super.onResume();
        onOnCameraChangeEvent(null);
    }

    @Override
    public void onStart() {
        super.onStart();
        binding.floatingActionSnap.requestFocus();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
        isInitView = false;
        EventBus.getDefault().unregister(this);
    }

    // 录像状态改变(0-手动开始录像,1-低电量停止录像,2-手动停止录像,3-自动录制下一段视频)
    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void onOnCameraChangeEvent(CameraChangeEvent event) {
        if (mCameraHelper.isRecording()) {
            binding.tvActionRecording.setVisibility(View.VISIBLE);
            binding.floatingActionRecord.setImageResource(R.drawable.floating_action_btn_stop_record);
        } else {
            binding.tvActionRecording.setVisibility(View.GONE);
            binding.floatingActionRecord.setImageResource(R.drawable.floating_action_btn_start_record);
        }
    }


}